const log = require('../util/log')

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, (reqObj, resolve) => {
    const mongoMng = require('../mng/mongoMng')
    if (!mongoMng.db) {
      log('mongo未连接')
      resolve({
        result: -1,
        msg: '系统错误'
      })
      return
    }
    if (!reqObj.openid) {
      resolve({
        result: -1,
        msg: '缺少openid'
      })
      return
    }
    // if (!reqObj.cardId) {
    //   resolve({
    //     result: -1,
    //     msg: '缺少cardId'
    //   })
    //   return
    // }
    mongoMng.db.collection('card').find({
      openid: reqObj.openid,
      // cardId: reqObj.cardId
    }).toArray((err, result) => {
      if (err) {
        log(err);
        resolve({
          result: -1,
          msg: '系统错误'
        })
        return;
      }
      var list = []
      result.forEach(item => {
        list.push({
          cardId: item.cardId,
          code: item.code
        })
      })
      resolve(list)
    })
  })
}
